import { Component } from '@angular/core';
import { FormControl, FormGroup, NonNullableFormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
	selector: 'app-passport-login',
	templateUrl: './login.component.html',
	styleUrls: ['./login.component.less'],
})
export class PassportLoginComponent {
	validateForm: FormGroup<{
		userName: FormControl<string>;
		password: FormControl<string>;
		remember: FormControl<boolean>;
	}> = this.fb.group({
		userName: ['', [Validators.required]],
		password: ['', [Validators.required]],
		remember: [true]
	});

	submitForm(): void {
		if (this.validateForm.valid) {
			console.log('submit', this.validateForm.value);
			this.messageSrc.info("成功登录");
			this.router.navigateByUrl('/')
		} else {
			Object.values(this.validateForm.controls).forEach(control => {
				if (control.invalid) {
					control.markAsDirty();
					control.updateValueAndValidity({ onlySelf: true });
				}
			});
		}
	}

	constructor(private fb: NonNullableFormBuilder, private messageSrc: NzMessageService, private router: Router) { }
}
